package com.ejoysoft.servlet;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.sql.SQLException;
import java.util.Vector;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.ejoysoft.common.Globa;
import com.ejoysoft.wlanyz.business.Ap;
import com.ejoysoft.wlanyz.business.Ac;
import com.ejoysoft.wlanyz.business.Comunity;
import com.sun.corba.se.impl.util.Version;

import java.io.*;
public class UpdateAPKServlet extends HttpServlet implements Servlet
{//*************使用此接口必须在tomcat文件夹下wirelessnet文件夹下建立文件UpdateAPK*********************
	private String aString = "";
	private static final long serialVersionUID = 217251451801586160L;
	public UpdateAPKServlet()
	{
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException {	// TODO Auto-generated method stub
		try {
			
		this.execute(req, resp);
		}catch(Exception e) {
			e.printStackTrace();
			throw new IOException(e.getMessage());
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		System.err.println("12");
		this.doGet(req, resp);
	}

	private void execute(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{    HttpSession session = req.getSession();         
	     ServletContext  application  = session.getServletContext();         
	     String Path = application.getRealPath ("/"); 
	     //+"myDoc/ContributesBoxAtt/"+bean.getAttRealName();         
	     Path = Path.replace("/", File.separator);
	     String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+"/";
		 Globa globa = new Globa(); 
		 StringBuffer sbReturn = new StringBuffer("<?xml version='1.0' encoding='utf-8'?> ");
		 StringBuffer sbReturnContent = new StringBuffer();
			req.setCharacterEncoding("utf-8");		
			resp.setCharacterEncoding("utf-8");	
			try
			{
			 sbReturn.append("<Root>");
			 sbReturnContent.append("<apinfo>");
		     sbReturn.append(returnApInfo(Path,basePath));
		     sbReturnContent.append("</apinfo>");
		     sbReturn.append("</Root>");
				
			} catch (Exception e)
			{
				e.printStackTrace();
			} finally
			{
				globa.closeCon();
				resp.getWriter().println(sbReturn.toString());
			}
 
	  }
	


	private StringBuffer returnApInfo (String path,String bathpath) throws SQLException
	{   
		      String pathaddString=path+"UpdateAPK";
		      
		       Globa globa = new Globa();
		       String filepath =filefind(pathaddString);
		       System.out.println("sssssssssssssssss"+filepath);
		       String str[]=filepath.split("version");
		       String version=str[1].substring(0,str[1].length()-4);
		       String str1[]=filepath.split("\\\\");
		       String url=bathpath+str1[3]+"/"+str1[4]+"/"+str1[5];
				StringBuffer sbReturnContent = new StringBuffer();
					sbReturnContent.append("<apinfo>");
					sbReturnContent.append("<version>" +version+ "</version>");
					sbReturnContent.append("<url>" + url+ "</url>");
					sbReturnContent.append("<description>" +""+ "</description>");
					sbReturnContent.append("</apinfo>");	
				
				globa.closeCon();
				return sbReturnContent;
	}
			
	
		public String filefind(String path)
		{ 
		
			String url=path.replaceAll("\\\\","\\\\\\\\");
			File file = new File(path);
			String filepath=method(file);
			System.out.println("输出filepath："+filepath);
			return filepath;
		}

		public String method(File f)
		{   
			
			File[] FList = f.listFiles();
			for (int i = 0; i < FList.length; i++)
			{
				if (FList[i].isDirectory()==true)
				{
					method(FList[i]);
					
				}
				else
				{
					String url=FList[i].getAbsolutePath();
					System.out.println("输出URL："+url);
					if(url.contains("zip"))
					{
					aString=url;
					}
				}
			}
			return aString;
			
		}
	
			 
}
	
